package com.neusoft.Shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.Shixun.dao.CareItemDao;
import com.neusoft.Shixun.dao.ClientCareItemDao;
import com.neusoft.Shixun.dao.ClientCareSettingDao;
import com.neusoft.Shixun.dto.ClientCareItemDto;
import com.neusoft.Shixun.po.*;
import com.neusoft.Shixun.service.ClientCareItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClientCareItemServiceImpl implements ClientCareItemService {

    @Autowired
    private ClientCareItemDao clientCareItemDao;

    @Autowired
    private CareItemDao careItemDao;

    @Autowired
    private ClientCareSettingDao clientCareSettingDao;


    @Override
//    获取所有项目
    public ResponseBean<List<CareItem>> getAllCareItems() {
        List<CareItem> careItems = careItemDao.selectList(null);
        return new ResponseBean<>(careItems);
    }

    @Override
    public ResponseBean<ClientCareItem> getClientCareItemById(Integer clientItemId) {
        ClientCareItem clientCareItem = clientCareItemDao.selectById(clientItemId);
        if (clientCareItem != null) {
            return new ResponseBean<>(clientCareItem);
        } else {
            return new ResponseBean<>(404, "客户护理项目关系未找到");
        }
    }

    @Override
//    给客户添加没有的护理项目
    public ResponseBean<Integer> addClientCareItem(ClientCareItem clientCareItem) {
        ClientCareSetting clientCareSetting = clientCareSettingDao.selectOne(new QueryWrapper<ClientCareSetting>().eq("client_id", clientCareItem.getClientId()));
        clientCareItem.setLevelId(clientCareSetting.getLevelId());
        int result = clientCareItemDao.insert(clientCareItem);
        return new ResponseBean<>(result); // 返回插入成功的行数
    }

    @Override
//    续费客户项目
    public ResponseBean<Integer> updateClientCareItem(ClientCareItem clientCareItem) {
        ClientCareItem clientCareItem1 = clientCareItemDao.selectOne(new QueryWrapper<ClientCareItem>().eq("client_id", clientCareItem.getClientId()).eq("item_id", clientCareItem.getItemId()));
        clientCareItem.setClientItemId(clientCareItem1.getClientItemId());
        int result = clientCareItemDao.updateById(clientCareItem);
        return new ResponseBean<>(result); // 返回更新成功的行数
    }

    @Override
//    移除客户的护理项目
    public ResponseBean<Integer> deleteClientCareItem(Integer clientId, Integer itemId) {
        ClientCareItem clientCareItem = clientCareItemDao.selectOne(new QueryWrapper<ClientCareItem>().eq("client_id", clientId).eq("item_id", itemId));
        int result = clientCareItemDao.deleteById(clientCareItem);
        return new ResponseBean<>(result); // 返回删除成功的行数
    }

    @Override
//    批量给用户添加护理项目（应该给客户id和级别id？）
    public ResponseBean<Integer> addClientCareItemByGroup(Integer clientId, Integer levelId, List<ClientCareItem> clientCareItems) {

        int insertNumber = 0;
        for (ClientCareItem clientCareItem : clientCareItems) {
            clientCareItem.setClientId(clientId);
            clientCareItem.setLevelId(levelId);
            insertNumber += clientCareItemDao.insert(clientCareItem);
        }
        return new ResponseBean<>(insertNumber);
    }

    @Override
//    根据客户id查询客户的护理项目以及状态
    public ResponseBean<List<ClientCareItemDto>> getClientCareItemsByClientId(Integer clientId) {
        return new ResponseBean<>(clientCareItemDao.getClientCareItemsByClientId(clientId));
    }

    @Override
//    查询客户没有的护理项目
    public ResponseBean<List<CareItem>> getClientCareItemsClientNotHave(Integer clientId) {
// 先查询所有项目
        List<CareItem> careItems = careItemDao.selectList(null);

// 再查询客户有的项目
        List<ClientCareItem> clientCareItems = clientCareItemDao.selectList(new QueryWrapper<ClientCareItem>().eq("client_id", clientId));
        Set<Integer> clientItemIds = new HashSet<>();
        for (ClientCareItem clientCareItem : clientCareItems) {
            clientItemIds.add(clientCareItem.getItemId());
        }

// 从所有项目中排除客户已有的项目
        List<CareItem> careItemsClientNotHave = careItems.stream()
                .filter(careItem -> !clientItemIds.contains(careItem.getItemId()))
                .collect(Collectors.toList());


        return new ResponseBean<>(careItemsClientNotHave);
    }
}